package com.sanmina.route.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanmina.route.dao.RouteDao;
import com.sanmina.route.dao.RouteDaoImpl;
import com.sanmina.route.dto.TreeListDTO;
import com.sanmina.route.entity.RouteStep;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/searchRouteStep")
public class SearchRouteStepAction extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8878511622911796506L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		RouteDao dao = new RouteDaoImpl();
		
		int routeKey =0; 
		String routeName ="";
		
		if(req.getParameter("routeKey")==null||Integer.valueOf(req.getParameter("routeKey"))==0){
			routeName = req.getParameter("routeName");
			routeKey =dao.isRouteExist(routeName).getRouteKey(); 
		}else{
			routeKey=Integer.valueOf(req.getParameter("routeKey"));
		}
		
		List<RouteStep> routeSteps = new ArrayList<RouteStep>();
		List<TreeListDTO> listDTOs = new ArrayList<TreeListDTO>();
		if(routeKey!=0){
			routeSteps= dao.listAllRouteStepsByRouteKey(routeKey);
		}
		JSONObject jo = new JSONObject();
		
		if(routeSteps!=null){
			for (int i = 0; i < routeSteps.size(); i++) {
				TreeListDTO dto = new TreeListDTO();
				RouteStep step =routeSteps.get(i);
				if(step.getStep()%10!=0)
					continue;
				
				dto.setRouteKey(routeKey);
				
				dto.setStep(step.getStep());
				dto.setCreateUnit(step.getCreateUnit());
				dto.setInsertedRoute(step.getInsertedRoute());
				
				if(step.getInsertedRoute()==1){
					dto.setLocationKey(step.getLocationKey());
					dto.setLocationName(dao.searchRouteByrouteKey(step.getLocationKey()).getName());
				}else{
					dto.setLocationKey(step.getLocationKey());
					dto.setLocationName(dao.searchLocationByKey(step.getLocationKey()).getWorkstation());
				}
				if(step.getFailLocationKey()!=0){
					dto.setFailLocationKey(step.getFailLocationKey());
					dto.setFailLocationName(dao.searchLocationByKey(step.getFailLocationKey()).getWorkstation());
				}
				if(step.getPassLoctionKey()!=0){
					dto.setPassLoctionKey(step.getPassLoctionKey());
					dto.setPassLoctionName(dao.searchLocationByKey(step.getPassLoctionKey()).getWorkstation());
				}
				if(step.getFromLocationKey1()!=0){
					dto.setFromLocationKey1(step.getFromLocationKey1());
					dto.setFromLocationName1(dao.searchLocationByKey(step.getFromLocationKey1()).getWorkstation());
				}
				if(step.getFromLocationKey2()!=0){
					dto.setFromLocationKey2(step.getFromLocationKey2());
					dto.setFromLocationName2(dao.searchLocationByKey(step.getFromLocationKey2()).getWorkstation());
				}
				if(step.getFromLocationKey3()!=0){
					dto.setFromLocationKey3(step.getFromLocationKey3());
					dto.setFromLocationName3(dao.searchLocationByKey(step.getFromLocationKey3()).getWorkstation());
				}
				if(step.getFromLocationKey4()!=0){
					dto.setFromLocationKey4(step.getFromLocationKey4());
					dto.setFromLocationName4(dao.searchLocationByKey(step.getFromLocationKey4()).getWorkstation());
				}
				listDTOs.add(dto);
			}
			JSONArray ja = JSONArray.fromObject(listDTOs);
			System.out.println("****Fount Route Step!****");
			jo.put("routeSteps", ja);
			resp.setCharacterEncoding("UTF-8");
			resp.setHeader("Cache-Control", "no-cache");
			resp.setContentType("text/html;charset=UTF-8");
			resp.getWriter().print(jo);
			resp.getWriter().close();
		}else{
			System.out.println("Not Fount Route Step!");
			jo.put("error", "Not Fount Route Step!");
			resp.setCharacterEncoding("UTF-8");
			resp.setHeader("Cache-Control", "no-cache");
			resp.setContentType("text/html;charset=UTF-8");
			resp.getWriter().print(jo);
			resp.getWriter().close();
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
